package com.book.bigwork.Webservice;

import com.book.bigwork.model.Book;
import com.book.bigwork.model.Order;
import com.book.bigwork.model.OrderItems;
import com.book.bigwork.model.User;
import com.book.bigwork.repository.OrderRepository;
import com.book.bigwork.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;
import org.springframework.web.context.WebApplicationContext;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

@Service
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode =
        ScopedProxyMode.TARGET_CLASS)
public class OrderService {
    private final OrderRepository orderRepository;
    private final UserRepository userRepository;
    private Order order = new Order();
    @Autowired
    public OrderService(OrderRepository orderRepository, UserRepository
            userRepository) {
        this.orderRepository = orderRepository;
        this.userRepository = userRepository;
    }
    public void AddBookToOrder(Book book) //添加图书到订单
    {
        BigDecimal totalPrice = new BigDecimal(0);
        BigDecimal orderItemPrice = new BigDecimal(0);
        int isFoundDuplidatedBook = 0;
        for (OrderItems orderItem : order.getBooks())
        {
            if(orderItem.getBook().equals(book)){
                isFoundDuplidatedBook = 1;
                orderItem.setQuantity(orderItem.getQuantity()+1);
                break;
            }
        }
        if (0 == isFoundDuplidatedBook) {
            order.addBook(book, 1L);
        }
        orderItemPrice = book.getBookPrice();
        if (order.getOrderPrice()==null) {
            totalPrice = orderItemPrice;
        }else {
            totalPrice = order.getOrderPrice().add(orderItemPrice);
        }
        order.setOrderPrice(totalPrice);
    }
    public void removeBookFromOrder(Book book) { //从订单中删除图书
        BigDecimal totalPrice = new BigDecimal(0);
        BigDecimal orderItemPrice = new BigDecimal(0);
        for (OrderItems orderItem : order.getBooks())
        {
            if(orderItem.getBook().equals(book)){
                orderItemPrice = new BigDecimal(orderItem.getQuantity()).
                        multiply(book.getBookPrice());
                break;
            }
        }
        order.removeBook(book);
        totalPrice = order.getOrderPrice().subtract( orderItemPrice);
        order.setOrderPrice(totalPrice);
    }
    public void createOrder(String userName) { //生成订单
        User user = userRepository.findByUserName(userName);
        order.setUser(user);
        order.setOrderDate(Calendar.getInstance().getTime());
        orderRepository.save(order);
    }
    public List<Order> findAllOrders() { //查询所有订单
        return orderRepository.findAll();
    }
    //查询订单状态
    public List<Order> findByOrderStatus(boolean orderStatus) {
        return orderRepository.findByOrderStatus(orderStatus);
    }
    public Order findOrderById(Long orderId) { //按订单号查询
        return orderRepository.findById(orderId).get();
    }
    public void updateOrder(Order order) { //编辑订单
        orderRepository.save(order);
    }
    public void deleteOrder(Order order) { //删除订单
        orderRepository.delete(order);
    }
    //按用户名查询订单
    public List<Order> findByUserName(String userName) {
        User user = userRepository.findByUserName(userName);
        return orderRepository.findByUser(user);
    }
    public Order getOrder() { //查询订单对象
    return order;
    }
    public Order clearOrder() { //消除订单对象
        order = new Order();
        return order;
    }
}